var Q = require('q'),
    operationController = require('../../../CBO/server/controllers/Operation.server.controller'),
    barcodeMasterController = require('../../../Barcode/server/controllers/BarcodeMaster.server.controller'),
    bizRecordController = require('../../../Barcode/server/controllers/BizRecord.server.controller'),
    
    workController = require('../../../CBO/server/controllers/Work.server.controller'),
    enumController = require('../../../QC/server/controllers/Enum.server.controller'),
    extendEnumController = require('../../../SystemManage/server/controllers/ExtendEnum.server.controller');


//标签打印添加
exports.submitTagPrint = function (req, res) {
    var parameter = req.body.Parameter,
        barcodeData = parameter.barcodeData,
        Terminal = parameter.Terminal,
        TerminalName = parameter.TerminalName,
        Work = parameter.Work,
        WorkName = parameter.WorkName,
        WorkLocation = parameter.WorkLocation,
        WorkLocationName = parameter.WorkLocationName,
        userId = parameter.userId;
        

    var dateNow = Date.now(),
        bizRecord = {
            CollectDateTime: dateNow,
            Users: [userId]
            
        };

    //根据物料和作业查找工序
    operationController.findByItemMasterAndWork(barcodeData.ItemMaster, Work, function (err, operationRecord) {
        if (err) {
            res.json({
                Data: null,
                Error: err
            });
        }
        else {
            if (operationRecord == null) {
                var newErr = new Error();
                newErr.leval = 9;
                newErr.title = '查询工序失败';
                newErr.message = '该物料没有找到对应作业的工序！';
                res.json({
                    Data: null,
                    Error: newErr
                });
            }
            else {
                // console.log("工序-------------"+operationRecord);
                bizRecord.BarcodeMaster = barcodeData._id;
                bizRecord.Work = Work;
                bizRecord.WorkName = WorkName;
                bizRecord.WorkLocation = WorkLocation;
                bizRecord.WorkLocationName = WorkLocationName;
                bizRecord.Terminal = Terminal;
                bizRecord.TerminalName = TerminalName;
                bizRecord.Operation = operationRecord._id;
                bizRecord.OperationName = operationRecord.Name;

                writebizRecord(barcodeData.Barcode, bizRecord).then(function (result) {
                    res.json(result);
                }, function (err) {
                    res.json(err);
                });
            }
        }
    });
};

//标签打印更新
exports.updateTagPrint = function (req, res) {
    var parameter = req.body.Parameter,
        barcodeData = parameter.barcodeData,
        userId = parameter.userId;
        

    var dateNow = Date.now();
    var updatebizRecord = {
        Users: [userId],
        CollectDateTime: dateNow
    };
    
    bizRecordController.updateBizRecordByValue(barcodeData.BizRecord._id, updatebizRecord, function (err, record) {
        if (err) {
            res.json({
                Data: null,
                Error: err
            });
        }
        else {
            res.json({
                Data: true,
                Error: err
            });
        }
    });
     
};


function writebizRecord(barcode, bizRecord) {
    var deferred = Q.defer();
    //添加流水信息
    bizRecordController.insert(bizRecord, function (err, record) {
        if (err) {
            deferred.reject(err);
            return;
        }
        console.log(record);
        var updateRecord = {
            Users: bizRecord.Users,
            BizRecord: record._id,
            Work: bizRecord.Work,
            WorkName: bizRecord.WorkName,
            WorkLocation: bizRecord.WorkLocation,
            WorkLocationName: bizRecord.WorkLocationName,
            Terminal: bizRecord.Terminal,
            TerminalName: bizRecord.TerminalName,
            Operation: bizRecord.Operation,
            OperationName: bizRecord.OperationName
        };
        // console.log(updateRecord);
        //更新主档信息
        barcodeMasterController.updateByValue(barcode, updateRecord, function (err, result) {
            if (err) {
                deferred.reject(err);
                return;
            }
            if (result.ok === 1) {
                deferred.resolve({
                    Data: true,
                    Error: null
                });
            }

        });
      
    });
    return deferred.promise;
}